class PhotosController < ApplicationController
  # GET /photos
  # GET /photos.json
  def index
    @photos = Photo.all
    
    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @photos }
    end
  end

  # GET /photos/1
  # GET /photos/1.json
  def show
    @photo = Photo.find(params[:id])
 
    respond_to do |format|
      format.html # show.html.erb
     
      format.json { render :json => @photo}
    end
  end

  # GET /photos/new
  # GET /photos/new.json
 
  def new
      @photo = Photo.new
      
      respond_to do |format|
        format.html # new.html.erb
      
      end 
  end
  

  # GET /photos/1/edit
  def edit
    @photo = Photo.find(params[:id])
  end

  # POST /photos
  # POST /photos.json
  def create
    @customization = Photo.new(params[:customization_id])
    @photo = @customization.photo.create(params[:photo])
      redirect_to customization_path(@customization)
    
  end

  # PUT /photos/1
  # PUT /photos/1.json
  def update  
    @photo = Photo.find(params[:id])

    respond_to do |format|
      if @photo.update_attributes(params[:photo])
        format.html { redirect_to @photo, :notice => 'Photo was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @photo.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /photos/1
  # DELETE /photos/1.json
  def destroy
    @customization = Customization.find(params[:customization_id])
    @photo = @customization.photos.find(params[:id])
      @photo.destroy
      redirect_to customization_path(@customization)
  end
  
  def display_image
      @image_data = Photo.find(params[:id])
        @image = @image_data.binary_data
      
    string_file = @image_data.filename;
    data = @image_data.binary_data;
    file = "string_file";
    File.open(file, "w"){ |f| f << data };
    send_data( data );
    puts data;
  end
end

